package com.demo.core.intercept;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.ext.*;

import java.io.IOException;

@Provider
public class TestInterceptor implements ReaderInterceptor, WriterInterceptor {
    /**
     * 读取body触发
     * @param context invocation context.
     * @return
     * @throws IOException
     * @throws WebApplicationException
     */
    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
        System.err.println("Before reading " + context.getGenericType());
        Object entity = context.proceed();
        System.err.println("After reading " + entity);
        return entity;
    }

    /**
     * 请求时触发
     * @param context invocation context.
     * @throws IOException
     * @throws WebApplicationException
     */
    @Override
    public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
        System.err.println("Before writing " + context.getEntity());
        context.proceed();
        System.err.println("After writing " + context.getEntity());
    }
}
